﻿using Dt261.Model;
using Dt261.ViewModel;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Dt261.Views
{
    /// <summary>
    /// SpeechWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SpeechWindow 
    {

        private bool tishiquit = true;
        public SpeechWindow(MemberInfo m=null)
        {
            InitializeComponent();

            this.Closing += Window_Closing;
            SpeechWindowViewModel model = new SpeechWindowViewModel();
            model.person = m; 
            this.DataContext = model;

            DMSkin.Core.Broadcast.RegisterBroadcast<string>("CLOSEWINDOW", (obj) =>
            {
                //TestWindowViewModel mo = DataContext as TestWindowViewModel;

                //mo.Dispose();
                this.tishiquit = false;
                this.Close();
            });
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

            if (this.tishiquit)
            {
                var result = MessageBoxX.Show("确认退出言语测试？", "提醒", Window.GetWindow(testExpress), MessageBoxButton.YesNo, new MessageBoxXConfigurations()
                {
                    MessageBoxStyle = MessageBoxStyle.Modern,
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });

                //MessageBoxResult result = MessageBox.Show("确认退出听力测试？", "询问", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {

                    e.Cancel = false;

                    SpeechWindowViewModel m = DataContext as SpeechWindowViewModel;

                    //+++
       

                    this.UninstallBroadcast();


                }


                //不关闭窗口
                if (result == MessageBoxResult.No)
                    e.Cancel = true;
            }
            else
            {
                e.Cancel = false;

                SpeechWindowViewModel m = DataContext as SpeechWindowViewModel;


      
                this.UninstallBroadcast();
            }


        }

        private void UninstallBroadcast()
        {
           
            DMSkin.Core.Broadcast.UninstallBroadcast("CLOSEWINDOW");
        
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            SpeechWindowViewModel m = DataContext as SpeechWindowViewModel;




            if(m.IsTesting == false)
            {
                if(m.AllResults == null || m.AllResults.Count() ==0)
                {
                    Notice.Show("未获取到测试表.", "提示", 3, MessageBoxIcon.Error);
                    return;
                }
            }
            
      
            //Command = "{Binding startEndCommand}"
            var handler = PendingBox.Show("", "提示", false, Window.GetWindow(XGrid)
                
            //    , new PendingBoxConfigurations()
            //{
            //    LoadingForeground = "#5DBBEC".ToColor().ToBrush(),
            //    ButtonBrush = "#5DBBEC".ToColor().ToBrush(),
            //}
                );


            m.startEndPlay();



            //handler.UpdateMessage("Almost complete (2/2)...");

            handler.Close();
        }
    }
}
